/*
 * @lc app=leetcode.cn id=599 lang=cpp
 *
 * [599] 两个列表的最小索引总和
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    vector<string> findRestaurant(vector<string> &list1, vector<string> &list2)
    {
        unordered_map<string, int> index;
        for (int i = 0; i < list1.size(); i++)
        {
            index[list1[i]] = i;
        }

        vector<string> ans;
        int sum = list1.size() + list2.size();


        for(int i = 0; i < list2.size(); i++)
        {
            if(index.count(list2[i]) > 0)
            {
                const int tmp = i + index[list2[i]];
                if(sum > tmp)
                {
                    sum = tmp;
                    ans.clear();
                    ans.push_back(list2[i]);
                }
                else if(sum == tmp)
                {
                    ans.push_back(list2[i]) ;
                }
                
            }
        }



        return ans;
    }
};
// @lc code=end
